﻿using app.Common;
using eService.BLL;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace eService.Presentation
{
    public partial class usrcCauHinh_HienThi : UserControl
    {
        private XmlDocument xmlDoc = new XmlDocument();
        public bool showPublic = false;
        ExcuteCAUHINH exe = new ExcuteCAUHINH();
        public usrcCauHinh_HienThi()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
            LoadThongSo();
        }
        private void LoadThongSo()
        {
            try 
            {
                string NgayDayDu = "", NgayThuGon = "", GioDayDu = "", GioThuGon = "", GiaTu = "", GiaDen = "", ChieuRong = "", ChieuCao = "", HienTheoNhom = "";
                ChieuRong = AppSetting.getSetting().getConfigValue("IconWidth");
                ChieuCao = AppSetting.getSetting().getConfigValue("IconHeight");
                NgayDayDu = AppSetting.getSetting().getConfigValue("NgayDayDu");
                NgayThuGon = AppSetting.getSetting().getConfigValue("NgayRutGon");
                GioDayDu = AppSetting.getSetting().getConfigValue("GioDayDu");
                GioThuGon = AppSetting.getSetting().getConfigValue("GioRutGon");
                HienTheoNhom = exe.getShowItemByGroup().ToString();
                GiaTu = exe.GetStartTime();
                GiaDen = exe.GetEndTime();
                if (GiaTu.Length != 2) GiaTu = GiaTu.PadLeft(2, '0');
                if (GiaDen.Length != 2) GiaDen = GiaDen.PadLeft(2, '0');

                numChieuCao.Value = int.Parse(ChieuCao);
                numChieuRong.Value = int.Parse(ChieuRong);
                txtNgayDayDu.Text = NgayDayDu;
                txtNgayThuGon.Text = NgayThuGon;
                txtGioDayDu.Text = GioDayDu;
                txtGioThuGon.Text = GioThuGon;
                cmbGiaDemDen.Text = GiaDen + " giờ";
                cmbGiaDemTu.Text = GiaTu + " giờ";
                if (HienTheoNhom == "1") chbHienTheoNhom.Checked = true;
                else chbHienTheoNhom.Checked = false;
                CONFIGDATEINFO obj = exe.GetObjDate();
                if (obj == null) return;
                if (obj.TypeDate == 1) rdbType1.Checked = true;
                else rdbType2.Checked = true;


            }
            catch { }
        }
        private void LuuThongSo()
        {
            try 
            {
                string NgayDayDu = "", NgayThuGon = "", GioDayDu = "", GioThuGon = "", GiaTu = "", GiaDen = "", ChieuRong = "", ChieuCao = "", HienTheoNhom ="", DinhDangGio="";
                NgayDayDu = txtNgayDayDu.Text;
                NgayThuGon = txtNgayThuGon.Text;
                GioDayDu = txtGioDayDu.Text;
                GioThuGon = txtGioThuGon.Text;
                GiaTu = cmbGiaDemTu.Text.Replace(" giờ", "");
                GiaDen = cmbGiaDemDen.Text.Replace(" giờ", "");
                ChieuRong = numChieuRong.Value.ToString();
                ChieuCao = numChieuCao.Value.ToString();
                if (chbHienTheoNhom.Checked) HienTheoNhom = "1";
                else HienTheoNhom = "0";

                if (rdbType1.Checked) DinhDangGio = "1";
                else DinhDangGio = "2";

                if (NgayDayDu != "" && NgayThuGon != "" && GioDayDu != "" && GioThuGon != "" && GiaTu != "" && GiaDen != "" && ChieuRong != "" && ChieuCao != "")
                {
                    AppSetting.getSetting().setConfigValue("IconWidth", ChieuRong);
                    AppSetting.getSetting().setConfigValue("IconHeight", ChieuCao);
                    AppSetting.getSetting().setConfigValue("NgayDayDu", NgayDayDu);
                    AppSetting.getSetting().setConfigValue("NgayRutGon", NgayThuGon);
                    AppSetting.getSetting().setConfigValue("GioDayDu", GioDayDu);
                    AppSetting.getSetting().setConfigValue("GioRutGon", GioThuGon);

                    UpdateKey("IconWidth", ChieuRong);
                    UpdateKey("IconHeight", ChieuCao);
                    UpdateKey("NgayDayDu", NgayDayDu);
                    UpdateKey("NgayRutGon", NgayThuGon);
                    UpdateKey("GioDayDu", GioDayDu);
                    UpdateKey("GioRutGon", GioThuGon);

                    int num = int.Parse(GiaTu);
                    int num2 = int.Parse(GiaDen);
                    string text = "";
                    if (num2 >= num)
                    {
                        for (int i = num; i <= num2; i++)
                        {
                            object obj = text;
                            text = string.Concat(new object[]
					{
						obj,
						",\"",
						i,
						"\""
					});
                        }
                    }
                    else
                    {
                        for (int i = num; i <= 24; i++)
                        {
                            object obj = text;
                            text = string.Concat(new object[]
					{
						obj,
						",\"",
						i,
						"\""
					});
                        }
                        for (int j = 1; j <= num2; j++)
                        {
                            object obj = text;
                            text = string.Concat(new object[]
					{
						obj,
						",\"",
						j,
						"\""
					});
                        }
                    }
                    text = text.Substring(1);
                    //bool KQ = false;
                    //KQ = exe.UpdateObjDate(short.Parse(DinhDangGio), text);
                    exe.setShowItemByGroup(int.Parse(HienTheoNhom));
                    ConfigurationManager.RefreshSection("appSettings");
                    MessageBox.Show("Thông tin đã được cập nhật vào hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
            }
            catch
            { }
        }
        public void UpdateKey(string strKey, string newValue)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            if (!this.KeyExists(strKey))
            {
                this.CreateKey(strKey);
            }
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    xmlNode2.Attributes["value"].Value = newValue;
                }
            }
            this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            this.xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
        public bool KeyExists(string strKey)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            bool result;
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    result = true;
                    return result;
                }
            }
            result = false;
            return result;
        }
        private void CreateKey(string strKey)
        {
            try
            {
                this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
                XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
                XmlNode xmlNode2;
                if (xmlNode.FirstChild != null)
                {
                    xmlNode2 = xmlNode.FirstChild.Clone();
                }
                else
                {
                    xmlNode2 = this.xmlDoc.CreateElement("add");
                    XmlAttribute node = this.xmlDoc.CreateAttribute("key");
                    XmlAttribute node2 = this.xmlDoc.CreateAttribute("value");
                    xmlNode2.Attributes.Append(node);
                    xmlNode2.Attributes.Append(node2);
                    xmlNode.AppendChild(xmlNode2);
                }
                xmlNode2.Attributes["key"].Value = strKey;
                xmlNode2.Attributes["value"].Value = "";
                xmlNode.AppendChild(xmlNode2);
                this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            }
            catch
            {
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            LuuThongSo();
        }
    }
}
